package tw.com.book.domain.dao;

import java.util.List;

import tw.com.book.componet.CursorUtils;
import tw.com.book.componet.DBContract;
import tw.com.book.componet.IssueBussinessException;
import tw.com.book.componet.Msg;
import tw.com.book.componet.VoUtils;
import tw.com.book.domain.V001Type;
import tw.com.book.domain.V002Type;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class V002DAO extends BaseDAO {

	private static final String TAG = V002DAO.class.getCanonicalName();

	public V002DAO(SQLiteDatabase sqlite, Context ct) {
		super(sqlite, ct);

	}

	public void insert(V002Type type) throws IssueBussinessException {

		try {
			final ContentValues map = VoUtils.getContentValues(type,
					this.getCt());

			this.getSqlite().insert(DBContract.V002_TABLE, null, map);
		} catch (IssueBussinessException e) {
			Log.v(TAG, e.getMessage());
			throw e;
		} catch (Exception e) {
			Log.v(TAG, e.getMessage());
			throw new IssueBussinessException(Msg.E003);
		}

	}

	public V002Type selectOne(String uuid) throws IssueBussinessException {

		final Cursor cur = this.getSqlite().rawQuery(
				"select * from v002 where uuid=?  order by serial_no desc;",
				new String[] { uuid });

		return CursorUtils.toConvertz(cur, V002Type.class, this.getCt()).get(0);

	}

	public List<V002Type> select(String uuid) throws IssueBussinessException {

		final Cursor cur = this.getSqlite().rawQuery(
				"select * from v002 where uuid=?  order by serial_no desc;",
				new String[] { uuid });

		return CursorUtils.toConvertz(cur, V002Type.class, this.getCt());
	}

	public List<V002Type> selectAll() throws IssueBussinessException {

		final Cursor cur = this.getSqlite()
				.rawQuery("select * from v002  order by serial_no desc;",
						new String[] {});

		return CursorUtils.toConvertz(cur, V002Type.class, this.getCt());
	}

	public int delete(String uuid) throws IssueBussinessException {
		return this.getSqlite().delete(DBContract.V002_TABLE, " uuid=?  ",
				new String[] { uuid });

	}

	public int delete(String uuid, String seqNo) throws IssueBussinessException {
		return this.getSqlite().delete(DBContract.V002_TABLE,
				" uuid=? and serial_no=?", new String[] { uuid, seqNo });
	}

	public int deleteGNo(String uuid, String seqNo)
			throws IssueBussinessException {
		return this.getSqlite().delete(DBContract.V002_TABLE,
				" uuid=? and serial_no>?", new String[] { uuid, seqNo });
	}

}
